using System;
using System.Collections.Generic;
using System.Data;
using System.Text;

namespace Watchdog2007 {
    public class DailySchedule : ScheduleBase {

        public DailySchedule(int id)
            : base(id) {
        }

        public DailySchedule ( DataRow drow ) : base ( drow ) { }
        #region proxy
        public new TimeSpan HourStart {
            get {
                return TimeSpan.Parse(base.HourStart);
            }
            set {
                base.HourStart = value.ToString();
            }
        }
        public new TimeSpan HourStop {
            get {
                return TimeSpan.Parse(base.HourStop);
            }
            set {
                base.HourStop = value.ToString();
            }
        }
        #endregion

        #region Comment
        //public DateTime GetNextExecuteTime ( DateTime date ) {
        //    DateTime Result = DateTime.MinValue;

        //    if (DateStart.Date.Add ( HourStart ) <= date && date <= DateStop.Date.Add ( HourStop )) {

        //        Result = date.Date.Add ( HourStart );
        //        while (Result <= date) {
        //            switch (RepeatType) {
        //                case WatchdogScheduleRepeatInterval.Minutes:
        //                    Result = Result.AddMinutes ( RepeatInterval );
        //                    break;
        //                case WatchdogScheduleRepeatInterval.Hours:
        //                    Result = Result.AddHours ( RepeatInterval );
        //                    break;
        //            }
        //        }
        //    } else {
        //        Result = DateTime.MaxValue;
        //    }
        //    return Result;
        //}
        #endregion

        public DateTime GetNextExecuteTime ( DateTime date ) {
            DateTime Result = DateTime.MinValue;
            TimeSpan now = date.TimeOfDay;
            if (DateStart.Date.Add(HourStart) <= date && date <= DateStop.Date.Add(HourStop)) {
                if (TimeSpan.Parse ( "00:00:00" ) <= date.TimeOfDay && date.TimeOfDay < HourStart) {
                    Result = date.Date.Add ( HourStart );
                }
                if (HourStart <= date.TimeOfDay && date.TimeOfDay <= HourStop) {
                    switch (RepeatType) {
                        case WatchdogScheduleRepeatInterval.Minutes:
                            // find the occurence that is in the future
                            Result = date.Date.Add ( HourStart );
                            while (Result <= date) {
                                Result = Result.AddMinutes ( RepeatInterval );
                                System.Diagnostics.Debug.WriteLine(Result.ToString());
                            }
                            // if the result is outside of the schedule we schedule it beginning of the next day
                            if (Result.Date == date.Date) {
                                // the next runtime is today
                                if (HourStart < Result.TimeOfDay && Result.TimeOfDay <= HourStop) {

                                } else {
                                    if (GetNextExecuteTime ( Result ) != DateTime.MaxValue)
                                        Result = date.Date.Add ( HourStart ).AddDays ( 1 );
                                    else
                                        Result = DateTime.MaxValue;
                                }
                            } else {
                                // the next runtime is tomorrow
                                Result = date.Date.Add ( HourStart ).AddDays ( 1 );
                            }
                            break;
                        case WatchdogScheduleRepeatInterval.Hours:
                            Result = date.Date.Add ( HourStart );
                            while (Result <= date) {
                                Result = Result.AddHours ( RepeatInterval );
                                System.Diagnostics.Debug.WriteLine(Result.ToString());
                            }
                            if (Result.Date == date.Date) {
                                // the next runtime is today
                                if (HourStart < Result.TimeOfDay && Result.TimeOfDay <= HourStop) {

                                } else {
                                    if (GetNextExecuteTime ( Result ) != DateTime.MaxValue)
                                        Result = date.Date.Add ( HourStart ).AddDays ( 1 );
                                    else
                                        Result = DateTime.MaxValue;
                                }
                            } else {
                                // the next runtime is tomorrow
                                Result = date.Date.Add ( HourStart ).AddDays ( 1 );
                            }
                            break;
                        case WatchdogScheduleRepeatInterval.None:
                            break;
                    }
                }
                if (HourStop < date.TimeOfDay && date.TimeOfDay <= TimeSpan.Parse("23:59:59")) {
                    Result = date.Date.Add ( HourStart ).AddDays ( 1 );
                }
            } else {
                // The schedule does not have tor run anymore
                // So we return maxdate.
                Result = DateTime.MaxValue;
            }
            return Result;
        }

        public new DateTime GetNextExecuteTime () {
            return GetNextExecuteTime ( DateTime.Now );
        }
        public DateTime SetNextExecuteTime () {
            NextRun = GetNextExecuteTime ( DateTime.Now );
            Update ( );
            return NextRun;
        }
        //public new DateTime GetNextExecuteTime() {
        //    DateTime Result = DateTime.MinValue;
        //    TimeSpan now = DateTime.Now.TimeOfDay;
        //    if (DateStart.Date < DateTime.Now && DateTime.Now < DateStop.Date) {
        //        if (HourStart < DateTime.Now.TimeOfDay && DateTime.Now.TimeOfDay < HourStop) {
        //            switch (RepeatType) {
        //                case WatchdogScheduleRepeatInterval.Minutes:
        //                    // find the occurence that is in the future
        //                    Result = DateTime.Today.Add(HourStart);
        //                    while (Result < DateTime.Now) {
        //                        Result = Result.AddMinutes(RepeatInterval);
        //                        System.Diagnostics.Debug.WriteLine ( Result.Tostring( ) );
        //                    }
        //                    // if the result is outside of the schedule we schedule it beginning of the next day
        //                    if (Result.Date == DateTime.Now.Date) {
        //                        // the next runtime is today
        //                        if (HourStart < Result.TimeOfDay && Result.TimeOfDay <= HourStop) {

        //                        } else {
        //                            Result = DateTime.Today.Add ( HourStart ).AddDays ( 1 );
        //                        }
        //                    } else {
        //                        // the next runtime is tomorrow
        //                        Result = DateTime.Today.Add ( HourStart ).AddDays ( 1 );
        //                    } 
        //                    //if (HourStart < Result.TimeOfDay && Result.TimeOfDay < HourStop) {
        //                    //    Result = DateTime.Today.Add(HourStart);
        //                    //}
        //                    break;
        //                case WatchdogScheduleRepeatInterval.Hours:
        //                    Result = DateTime.Today.Add(HourStart);
        //                    while (Result < DateTime.Now) {
        //                        Result = Result.AddHours(RepeatInterval);
        //                        System.Diagnostics.Debug.WriteLine ( Result.Tostring( ) );
        //                    }
        //                    if (Result.Date == DateTime.Now.Date) {
        //                        // the next runtime is today
        //                        if (HourStart < Result.TimeOfDay && Result.TimeOfDay <= HourStop) {

        //                        } else {
        //                            Result = DateTime.Today.Add ( HourStart ).AddDays ( 1 );
        //                        }
        //                    } else {
        //                        // the next runtime is tomorrow
        //                        Result = DateTime.Today.Add ( HourStart ).AddDays ( 1 );
        //                    }
        //                    break;
        //                case WatchdogScheduleRepeatInterval.None:
        //                    break;
        //            }
        //        }
        //    } else {

        //    }
        //    return Result;
        //}
    }
}
